package cn.crdeng.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 配置登录拦截器
 */
public class LoginHandlerInterceptor implements HandlerInterceptor {


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Object loginUsername = request.getSession().getAttribute("user");
        if (loginUsername==null){
            //未登陆，返回登陆页面
            request.setAttribute("message","没有权限,请登录");
            request.getRequestDispatcher("/admin").forward(request,response);
            return false;
        }


        //已登陆，放行请求
        return true;

    }
}
